package com.study.yygh.user.api;

import com.study.yygh.common.result.Result;
import com.study.yygh.common.utils.AuthContextHolder;
import com.study.yygh.model.user.Patient;
import com.study.yygh.user.service.PatientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author tao
 * @create 2021-10-25 11:05
 * @Description 就诊人管理接口
 */
@RestController
@RequestMapping("/api/user/patient")
public class PatientApiController {

    @Autowired
    private PatientService patientService;

    // 获取就诊人列表
    @GetMapping("auth/findAll")
    public Result findAll(HttpServletRequest request){
        // 获取当前登录用户id
        Long userId = AuthContextHolder.getUserId(request);
        List<Patient> list = patientService.findAllUserId(userId);
        return Result.ok(list);
    }

    // 添加就诊人
    @PostMapping("auth/save")
    public Result savePatient(@RequestBody Patient patient,HttpServletRequest request){
        // 获取当前登录者用户的id
        Long userId = AuthContextHolder.getUserId(request);
        patient.setUserId(userId);
        patientService.save(patient);
        return Result.ok();
    }

    // 根据id获取就诊人信息
    @GetMapping("auth/get/{id}")
    public Result getPatient(@PathVariable Long id){
        Patient patient = patientService.getPatientId(id);
        return Result.ok(patient);
    }

    // 修改就诊人
    @PutMapping("auth/update")
    public Result updatePatient(@RequestBody Patient patient){
        // 获取当前登录者用户的id
        patientService.updateById(patient);
        return Result.ok();
    }

    // 删除就诊人
    @DeleteMapping("auth/remove/{id}")
    public Result removePatient(@PathVariable Long id){
        // 获取当前登录者用户的id
        patientService.removeById(id);
        return Result.ok();
    }

    // 获取就诊人信息
    @ApiOperation(value =  "获取就诊人信息")
    @GetMapping("inner/get/{id}")
    public Patient getPatientOrder(@ApiParam(name = "id",value = "就诊人id",required = true)
                                   @PathVariable("id") Long id){
        return patientService.getPatientId(id);
    }
}
